package cn.iocoder.yudao.module.erp.controller.admin.purpurchaseorderitem;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.purpurchaseorderitem.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.purpurchaseorderitem.PurPurchaseOrderItemDO;
import cn.iocoder.yudao.module.erp.service.purpurchaseorderitem.PurPurchaseOrderItemService;

@Tag(name = "管理后台 - ERP 采购单项")
@RestController
@RequestMapping("/erp/pur-purchase-order-item")
@Validated
public class PurPurchaseOrderItemController {

    @Resource
    private PurPurchaseOrderItemService purPurchaseOrderItemService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP 采购单项")
    @PreAuthorize("@ss.hasPermission('erp:pur-purchase-order-item:create')")
    public CommonResult<Long> createPurPurchaseOrderItem(@Valid @RequestBody PurPurchaseOrderItemSaveReqVO createReqVO) {
        return success(purPurchaseOrderItemService.createPurPurchaseOrderItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 采购单项")
    @PreAuthorize("@ss.hasPermission('erp:pur-purchase-order-item:update')")
    public CommonResult<Boolean> updatePurPurchaseOrderItem(@Valid @RequestBody PurPurchaseOrderItemSaveReqVO updateReqVO) {
        purPurchaseOrderItemService.updatePurPurchaseOrderItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 采购单项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:pur-purchase-order-item:delete')")
    public CommonResult<Boolean> deletePurPurchaseOrderItem(@RequestParam("id") Long id) {
        purPurchaseOrderItemService.deletePurPurchaseOrderItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 采购单项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:pur-purchase-order-item:query')")
    public CommonResult<PurPurchaseOrderItemRespVO> getPurPurchaseOrderItem(@RequestParam("id") Long id) {
        PurPurchaseOrderItemDO purPurchaseOrderItem = purPurchaseOrderItemService.getPurPurchaseOrderItem(id);
        return success(BeanUtils.toBean(purPurchaseOrderItem, PurPurchaseOrderItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 采购单项分页")
    @PreAuthorize("@ss.hasPermission('erp:pur-purchase-order-item:query')")
    public CommonResult<PageResult<PurPurchaseOrderItemRespVO>> getPurPurchaseOrderItemPage(@Valid PurPurchaseOrderItemPageReqVO pageReqVO) {
        PageResult<PurPurchaseOrderItemDO> pageResult = purPurchaseOrderItemService.getPurPurchaseOrderItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PurPurchaseOrderItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 采购单项 Excel")
    @PreAuthorize("@ss.hasPermission('erp:pur-purchase-order-item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPurPurchaseOrderItemExcel(@Valid PurPurchaseOrderItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PurPurchaseOrderItemDO> list = purPurchaseOrderItemService.getPurPurchaseOrderItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 采购单项.xls", "数据", PurPurchaseOrderItemRespVO.class,
                        BeanUtils.toBean(list, PurPurchaseOrderItemRespVO.class));
    }

}